%macro get_f1099ssa;

	
	proc printto log = "&logdir/get_f1099ssa.txt" new;
	run;
	

		/****************************
		*	Get the tins 			*
		*	into 'tinlist'			*
		****************************/
		%make_tinlist;

		/****************************************
		*										*
		*			Start PROC SQL				*
		*										*
		****************************************/
		proc sql noprint;
		[connection details redacted]
			create table sample as
			select * from connection to iq 
			(
			select
				a.person_id,
				b.[redacted] as bens,
				b.[redacted] as ss_repay,
				b.[redacted] as wh,
				b.[redacted] as ss_type,
				b.[redacted] as formdate
			from #tinlist as a
				inner join [redacted] as b
					on(a.person_id = b.[redacted])
			where b.[redacted] = &base_yr
				and b.[redacted] > 0
				and b.[redacted] <= 4
			order by
				a.person_id,
				ss_type,
				formdate
			);
			disconnect from iq;
			quit;

		/****************************************
		*			Drop duplicates				*
		*			(keep most recent			*
		*			within payer_tin, tin)		*
		****************************************/
		data sample;
			set sample;
			by person_id ss_type;
			if last.ss_type;
			drop formdate;
			
			if bens = . then bens = 0;
			if ss_repay = . then ss_repay = 0;
			net_bens = max(0, bens - ss_repay);
		run;
		

		/********************************
		*								*
		*		Sum by individual		*
		*								*
		********************************/
		proc means data = sample nway noprint;
			class person_id;
			output out = rlib.f1099ssa(drop=_type_ _freq_)
				sum(net_bens wh) = gssi f1099ssa_wh;
		run;
		
		





	proc printto;
	run;
%mend;
